package com.android.lotto.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.lotto.R;
import com.android.lotto.adapter.MainPageAdapter;
import com.android.lotto.entity.TabEntity;
import com.android.lotto.fragment.union.UnionRadomFragment;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;

/**
 * Created by JS-00 on 2017/5/19.
 */

public class UnionChoiceFragment extends BaseFragment {
    private Context mContext;
    private View mView;

    private String[] mTitles = {"机选", "缩水", "杀号", "胆拖"};
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private CommonTabLayout mTabLayout_lotto;
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContext = getActivity();
        mView = inflater.inflate(R.layout.fragment_choice_number, null);
        mTabEntities.clear();
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], 0, 0));
        }
        initFragments();
        initView(mView);
        return mView;
    }

    private void initFragments() {
        mFragments.clear();
        UnionRadomFragment unionRandomFragment = new UnionRadomFragment();
        UnionRadomFragment unionContainFragment = new UnionRadomFragment();
        UnionRadomFragment unionKillFragment = new UnionRadomFragment();
        UnionRadomFragment unionLessFragment = new UnionRadomFragment();
        mFragments.add(unionRandomFragment);
        mFragments.add(unionContainFragment);
        mFragments.add(unionKillFragment);
        mFragments.add(unionLessFragment);

    }

    private void initView(View view) {
        mTabLayout_lotto = (CommonTabLayout)view.findViewById(R.id.tl_choice_number);
        setTabLayout(view);
    }

    private void setTabLayout(View view) {
        final ViewPager vp_choice_number = (ViewPager)view.findViewById(R.id.vp_choice_number);
        vp_choice_number.setAdapter(new MainPageAdapter(getFragmentManager(), mTitles, mFragments));

        mTabLayout_lotto.setTabData(mTabEntities);
        mTabLayout_lotto.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                vp_choice_number.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });

        vp_choice_number.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mTabLayout_lotto.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        vp_choice_number.setCurrentItem(0);
    }

}
